﻿using System.Threading;
using MassPokerTool.Infrastructure.Hooking.Data;
using MassPokerTool.Infrastructure.Hooking.Handlers;
using MassPokerTool.Infrastructure.Logging;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace MassPokerTool.Infrastructure.Hooking
{
    public class ProcessingEngine
    {
        private static readonly ILogger Log;
        private static readonly IUnityContainer Container;

        static ProcessingEngine()
        {
            Container = ServiceLocator.Current.GetInstance<IUnityContainer>();
            Log = new Logger(typeof(ProcessingEngine));
        }

        public static void Process(IHookData hookData)
        {
            ThreadPool.QueueUserWorkItem(ResolveAndRunHandler, hookData);
        }

        private static void ResolveAndRunHandler(object parameter)
        {
            var hookData = parameter as IHookData;
            if (hookData != null)
            {
                // all hook handlers must be registered with per thread lifetime manager
                var handler = (IHookHandler)Container.Resolve(hookData.HandlerType);
                handler.Handle(hookData);
            }
        }
    }
}
